package com.concorrency.learn.controller;

import com.concorrency.learn.service.PolymerizationService;
import com.concorrency.learn.vo.AAA;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

/**
 * Created by corey on 2018/9/8.
 */

@Controller
@RequestMapping("user")
public class UserController {

    @Autowired
    private PolymerizationService service;

    @RequestMapping("/getUser/{id}")
    @ResponseBody
    public AAA getUser(@PathVariable("id")int id){
        return service.getAaa(id);
    }

    @RequestMapping("/getUserByConcorren/{id}")
    @ResponseBody
    public AAA getUserByConcorren(@PathVariable("id")int id){
        AAA aaa = null;
        Callable<AAA> getUser = new Callable<AAA>() {
            @Override
            public AAA call() throws Exception {
                System.out.println("进入call方法");
                return service.getAaaByConcorrent(id);
            }
        };
        FutureTask<AAA> getAaa = new FutureTask<AAA>(getUser);
        new Thread(getAaa).start();
        try {
            aaa = getAaa.get();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
        System.out.println("主线程已完成");
        return aaa;
    }
}
